package com.aaa.controller.jl;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.pojo.TesterVo;
import com.aaa.pojo.ViewTestPlan;
import com.aaa.pojo.jl.*;
import com.aaa.service.jl.QuestionSasService;
import com.aaa.service.jl.TesterSasService;
import com.aaa.service.sys.ViewTestPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class FrontController {

    @Autowired
    private TesterSasService testerSasService;
    @Autowired
    private ViewTestPlanService viewTestPlanService;
    @Autowired
    private QuestionSasService questionSasService;
    ResltBean resultBean = new ResltBean();

    @RequestMapping("/checkTest")
    public ResltBean checkTest(@RequestBody TesterVo tester , HttpSession session) {
        System.out.println(tester);
        ViewTestPlan testPlan = viewTestPlanService.findByTestCode(tester.getTestCode());
        if (ObjectUtil.isNotEmpty(testPlan)) {
                Date date = new Date();
            if (date.after(testPlan.getTestBegin()) && date.before(testPlan.getTestEnd())) {
                boolean flag = false;
                switch (testPlan.getType()) {
                    case "fpa":
                        List<Map<String, Object>> byPhone = testerSasService.phoneFpa(tester.getPhone());
                        if (ObjectUtil.isNotEmpty(byPhone) && byPhone.size() > 0) {
                            flag = true;
                        }
                        break;
                    case "sas":
                        List<Map<String, Object>> byPhone1 = testerSasService.phoneSas(tester.getPhone());
                        if (ObjectUtil.isNotEmpty(byPhone1) && byPhone1.size() > 0) {
                            flag = true;
                        }
                        break;
                    case "sds":
                        List<Map<String, Object>> byPhone2 = testerSasService.phoneSds(tester.getPhone());
                        if (ObjectUtil.isNotEmpty(byPhone2) && byPhone2.size() > 0) {
                            flag = true;
                        }
                }
                if (flag) {
                    resultBean.setCode(1);
                    resultBean.setMsg("对不起,您的手机号已经测试过了!");
                } else {
                    resultBean.setCode(0);
                    tester.setId(testPlan.getId());
                    tester.setType(testPlan.getType());
                    tester.setTestName(testPlan.getTestName());
                    session.setAttribute("tester", tester);
                }
            } else {
                resultBean.setCode(1);
                resultBean.setMsg("对不起,时间已过");
            }
        } else {
            resultBean.setCode(1);
            System.out.println(testPlan);
            resultBean.setMsg("对不起,邀请码无效,请重新确认");
        }
        return resultBean;
    }
    @RequestMapping("/findAllQuestion")

    public ModelAndView findAllQusertion(HttpSession session , ModelAndView modelAndView) {
        TesterVo tester = (TesterVo) session.getAttribute("tester");
        String type = tester.getType();

        if ("sas".equals(type)) {
            List<QusetionSas> qusetionList = questionSasService.findAllQusertion();
            modelAndView.addObject("questionList", qusetionList);
        }

        modelAndView.setViewName("testQuestion");
        return modelAndView;
    }
    @RequestMapping("/saveBatchTestResult")
    public ResltBean saveBatchTestResult(@RequestBody List<Map<String,Object>> resultList,HttpSession session) {
        TesterVo tester = (TesterVo)session.getAttribute("tester");
        String testResult;
        if (tester.getType().equals("sas")) {
            testResult = testerSasService.saveTestResult(resultList, tester);
            resultBean.setCode(0);
            resultBean.setMsg("");
            resultBean.setData(testResult);
            return resultBean;
        }
        return null;
    }

}